<?php
require_once 'Panel/PanelModel.php';
require_once 'Panel/PanelView.php';
require_once 'Panel/Files.php';
class PanelController{
	private $_parameters;
	private $action;
	private $sectionFilesPath;
	
	function __construct($__parameters=null){
		$this->action = $__parameters;
		$this->_parameters = explode(',',isset($_REQUEST['parameters'])? $_REQUEST['parameters']: "");
		$this->sectionFilesPath  = "../public/users/".str_replace('www.','',$_SERVER['HTTP_HOST'])."/files/";
	}
	
	public function indexAction($__parameters=null){
		global $objSession;
		if ($objSession->IsLoggedIn()){

			$this->listastronAction();
		}else{
			tools::_redirect('zaloguj_sie.html');
			return;
		}
	}
	
	public function addpageAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		PanelView::add_new_page($this->_parameters[2]);
	}

	public function showpageAction($id_page){
		global $config;
		$__page = PanelModel::get_page_data($id_page,$config->__lang);
		tools::_assign("viewp",$__page);
	}
	
	public function editpageAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}		
		$__page = PanelModel::get_page_data($this->_parameters[2],$this->_parameters[3]);
		$__sections = PanelModel::get_page_sections($this->_parameters[2],$this->_parameters[3]);				
		PanelView::edit_one_page($__page,$__sections);
	}

	public function listastronAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		$grupy__ = unserialize(UMENU);
		
		foreach ($grupy__ as $id_grup => $nazwa_grupy) {
			tools::_assign("nazwa_menu",$nazwa_grupy);
			$__pages = PanelModel::get_users_pages_list($__params,$id_grup);				
			PanelView::schow_main_pages_list($__pages);
		}
		PanelView::CMSSettingsForm();
	}
	
	public function updownAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		switch($this->_parameters[2]){
			case 'up':
				PanelModel::up_lp_category($this->_parameters[4],$this->_parameters[5]);
				break;
			case 'down':
				PanelModel::down_lp_category($this->_parameters[4],$this->_parameters[5]);
				break;		
		}
	}
	
	public function savepageAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		$__result = PanelModel::save_upadate_page_data();
	}
	
	public function delpageAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		$__result = PanelModel::delete_page_data($this->_parameters[2]);
		
		if ($__result){
			header("Location: zarzadzaj_stronami.html");
			tools::_assign('updated',1);
		}else{
			tools::_assign('updated',0);
		}			
	}
	
	public function savenewpageAction(){
		global $config,$objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}		
		$__result = PanelModel::save_new_page_data();
		if ($__result){
			tools::_assign('updated',1);
			header("Location: edytuj_strone,,,{$config->id_page},{$config->id_lang}.html");
		}else{
			tools::_assign('updated',0);
		}
	}
	
	public function newsectionAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}
		$__section = array("id_page" =>$this->_parameters[0],"id_section"=>$this->_parameters[1],"id_lang"=>$this->_parameters[2]);
		PanelView::new_section_form($__section);		
	}
	
	public function savenewsecAction(){
		global $config,  $objSession;;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}		
		$__result = PanelModel::save_new_section_data();
		header("Location: edytuj_strone,,,{$config->id_page},{$config->id_lang}.html");
	}
	
	public function editsectAction($view=""){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}		
		$__section = PanelModel::get_section_data($this->_parameters);
		if (empty($__section)){
			tools::_redirect("edytuj_strone,{$this->_parameters[0]},{$this->_parameters[2]}.html","Sekcja została usunięta...");
			break;
		}		 
		if ( array_key_exists(3,$this->_parameters) ) {
			$__extenction_name =  $this->_parameters[3];
		} else{
			$__extenction_name =  $__section["id_exten"];
		}
		$file = new FilesController($this->sectionFilesPath,$this->_parameters[0].'_'.$this->_parameters[1].'_');
		$group = $file->getFiles();
		if(!empty($group['photos']))tools::_assign('fFotos',$group['photos']);
		if(!empty($group['archiw']))tools::_assign('fArch',$group['archiw']);
		if(!empty($group['doc']))tools::_assign('fDoc',$group['doc']);
		if(!empty($group['video']))tools::_assign('fVideo',$group['video']);
		if(!empty($group['audio']))tools::_assign('fAudio',$group['audio']);
		PanelView::edit_one_section($__section,$__extenction_name,$view);		
	}
	
	public function saveedisecAction($redirect=null){
		global $config, $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}		
		$__result = PanelModel::save_upadate_section_data();
		
		// zapis pliku: path, prefix_width_height_
		$file = new FilesController($this->sectionFilesPath,$config->id_page.'_'.$config->id_section.'_');
		$file->saveFiles();
		if($redirect == "user")header("Location: sekcja,{$config->id_page},{$config->id_section},{$config->id_lang}.html");
			
	}
	
	public function delsecAction(){
		global $objSession;
		if ( !$objSession->IsLoggedIn()){	tools::_redirect('zaloguj_sie.html'); return;}		
		$__result = PanelModel::delete_section_data_from_page($this->_parameters[2],$this->_parameters[3],$this->_parameters[4]);
		header("Location: edytuj_strone,,,{$this->_parameters[2]},{$this->_parameters[4]}.html");
	}
}
?>